/**This class defines the XML-to-HTML transformer that can read in a SILK
 * file and output an HTML file formatted according to an XSL stylesheet.
 *
 * The basic code is modeled after the Oracle (Sun) Java tutorial example
 * called "Stylizer.java" associated with the lesson "Transforming XML Data with
 * XSLT" in "Java API for XML Processing (JAXP) > Extensible Stylesheet Language
 * Transformations". Their code is (c) 2006 Sun Microsystems, Inc.
 *
 * @author  Gary Morris		garymorris2245@verizon.net
 *
 * Created on Feb 24, 2011
 */

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class XMLTransformer {

    public void transform(File datafile, File stylesheet, File outfile) {

        try {
            //  Parse data file into a DOM for input
//            DocumentBuilder builder = factory.newDocumentBuilder();
//            Document document = builder.parse(datafile);
            // Use a Transformer for output
            TransformerFactory tFactory = TransformerFactory.newInstance();
            StreamSource stylesource = new StreamSource(stylesheet);
            Transformer transformer = tFactory.newTransformer(stylesource);
            StreamSource source = new StreamSource(datafile);
            StreamResult result = new StreamResult(outfile);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException tce) {
            // Error generated by the parser
            System.out.println("\n** Transformer Factory error");
            System.out.println("   " + tce.getMessage());
            // Use the contained exception, if any
            Throwable x = tce;
            if (tce.getException() != null) {
                x = tce.getException();
            }
            x.printStackTrace();
        } catch (TransformerException te) {
            // Error generated by the parser
            System.out.println("\n** Transformation error");
            System.out.println("   " + te.getMessage());
            // Use the contained exception, if any
            Throwable x = te;
            if (te.getException() != null) {
                x = te.getException();
            }
            x.printStackTrace();
        }
    } // main
}
